package com.xuerhua.yxt.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.xuerhua.yxt.model.SysMenu;
import com.xuerhua.yxt.model.SysResource;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.StringUtil;

public class AdminUser {

	private TeacherInfo teacherInfo;
	
	private List<SysMenu> sysMenuList;
	
	private Map<String, SysMenu> sysMenuMap;

	public TeacherInfo getTeacherInfo() {
		return teacherInfo;
	}

	public void setTeacherInfo(TeacherInfo teacherInfo) {
		this.teacherInfo = teacherInfo;
	}

	public List<SysMenu> getSysMenuList() {
		return sysMenuList;
	}

	public void setSysMenuList(List<SysMenu> sysMenuList) {
		this.sysMenuList = sysMenuList;
	}

	public Map<String, SysMenu> getSysMenuMap() {
		return sysMenuMap;
	}

	public void setSysMenuMap(Map<String, SysMenu> sysMenuMap) {
		this.sysMenuMap = sysMenuMap;
	}
	
	public void fillMenuInfo(List<SysResource> list) {
		Map<String, SysMenu> sysMenuMap = new HashMap<>();
		Map<String, List<SysMenu>> menuMap = new HashMap<String, List<SysMenu>>();
		List<SysMenu> sysMenuList = null;
		if(list != null) {
			sysMenuList = BeanUtils.copyAs(list, SysMenu.class);
			for(SysMenu menu : sysMenuList) {

				if(StringUtils.isNotBlank(menu.getResourceUrl())) {
					//有url地址就放到sysMenuMap里面，url为key，menu对象为value
					sysMenuMap.put(menu.getResourceUrl(), menu);
				}
				String key = null;
				Integer parentId = menu.getParentId();
				//如果parentId是0或者空的话，则它自身是根目录
				//否则就是子目录
				if(!(parentId != null && parentId != 0)) {
					key = Constants.ROOT_MENU_KEY;	//"ROOT"
				}else {
					key = String.valueOf(parentId);
				}
				List<SysMenu> childMenuList = menuMap.get(key);
				if(childMenuList == null) {
					childMenuList = new ArrayList<SysMenu>();
				}
				childMenuList.add(menu);
				menuMap.put(key, childMenuList);
			}
			
			for(SysMenu menu : sysMenuList) {
				menu.setSubMenuList(menuMap.get(String.valueOf(menu.getId())));
				if(!StringUtil.isNullOrEmpty(menu.getSubMenuList())) {
					menu.setHasChild(true);
				}
			}
			sysMenuList = menuMap.get(Constants.ROOT_MENU_KEY);
		}
		this.sysMenuList = sysMenuList;
		this.sysMenuMap = sysMenuMap;
	}
	
}
